<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="/layui/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/layui/lib/font-awesome-4.7.0/css/font-awesome.css" media="all">

</head>
<body>
<form class="layui-form" >
    <div class="layui-form-item">
        <label class="layui-form-label">顶级分类</label>
        <div class="layui-input-inline">
            <input type="text"name="pid"value="0" id="tree" lay-filter="tree" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">权限名称</label>
        <div class="layui-input-inline">
            <input type="text" name="title" required lay-verify="required"  class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">权限url</label>
        <div class="layui-input-inline">
            <input type="text" name="href" required   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">单选框</label>
        <div class="layui-input-block">
            <input type="radio" name="is_menu" value="0" title="目录">
            <input type="radio" name="is_menu" value="1" title="按钮" checked>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">权限图标</label>
        <div class="layui-input-inline">
            <input type="text" id="iconPicker"name="icon" value="" lay-filter="iconPicker" style="display:none;">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">添加权限</button>
        </div>
    </div>
</form>
<script src="/layui/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/layui/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="/js/lay-module/iconPicker/iconPickerFa.js"></script>
<script>
    //3. 加载formSelect模块
    layui.config({
        base: "/js/"
    }).extend({
        treeSelect: "treeSelect"
    });
    layui.use(['jquery', 'layer','form','treeSelect'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer;
        //4. 初始化
        var treeSelect = layui.treeSelect;
        treeSelect.render({
            // 选择器
            elem: '#tree',
            // 数据
            data: '/admin/auth_select',
            // 异步加载方式：get/post，默认get
            type: 'post',
            // 占位符
            placeholder: '顶级分类',
            // 是否开启搜索功能：true/false，默认false
            search: false,
            style:{
                folder:{enable: false},
                line:{enable: true}
            },
            // 点击回调
            click: function(d){
                $("#tree").val(d.current.id);
            },
            // 加载完成后的回调函数
            success: function (d) {
            }
        });
        var iconPicker = layui.iconPickerFa;
        iconPicker.render({
            // 选择器，推荐使用input
            elem: '#iconPicker',
            // fa 图标接口
            url: "/lib/font-awesome-4.7.0/less/variables.less",
            // 是否开启搜索：true/false，默认true
            search: true,
            // 是否开启分页：true/false，默认true
            page: true,
            // 每页显示数量，默认12
            limit: 12,
            // 每个图标格子的宽度：'43px'或'20%'
            cellWidth: '43px',
            // 点击回调
            click: function (data) {
                console.log(data);
            },
            // 渲染成功后的回调
            success: function(d) {
                console.log(d);
            }
        });
        //监听提交
        form.on('submit(formDemo)', function (data) {
            var result = data.field;//JSON.stringify(data.field);
            $.ajaxSetup({
                headers: { 'X-CSRF-TOKEN' : '{{ csrf_token() }}' }
            });
            $.ajax({
                url : "/admin/auth_insert",
                type : "post",
                async: false , //改为同步
                data:result,
                success : function(data) {
                    if(data==1){
                        parent.layer.closeAll();
                        parent.location.reload();
                    }
                },
            });
        });
    });
</script>
</body>
</html>
